package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Scenicspot;
import cn.wolfcode.luowowo.article.domain.Subject;
import cn.wolfcode.luowowo.article.domain.Ticket;
import cn.wolfcode.luowowo.article.domain.Tourist;
import cn.wolfcode.luowowo.article.query.TouristQuery;
import cn.wolfcode.luowowo.article.service.IScenicspotService;
import cn.wolfcode.luowowo.article.service.ISubjectService;
import cn.wolfcode.luowowo.article.service.ITicketService;
import cn.wolfcode.luowowo.article.service.ITouristService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("")
public class TicketController {

    @Reference
    private ITouristService touristService;

    @Reference
    private ISubjectService subjectService;

    @Reference
    private ITicketService ticketService;

    @Reference
    private IScenicspotService scenicspotService;

    @RequestMapping("ticket")
    public String index(Model model, @ModelAttribute("qo")TouristQuery qo){
        List<Subject> subject = subjectService.selectByDestId(3261L);
        model.addAttribute("subjects",subject);

        //通过目的地id 查询景点
        Long sid = subject.get(0).getId();
        List<Tourist> list = touristService.listByDestIdAndSubjectId(3261L,sid);
        if(list.size() > 8){
            list = list.subList(0, 8);
        }
        model.addAttribute("tickets",list);

        List<Tourist> ticketsTop4 = touristService.listForList(qo);
        if(ticketsTop4.size() > 4){
            ticketsTop4 = ticketsTop4.subList(0, 4);
        }
        model.addAttribute("ticketsTop4",ticketsTop4);


        return "ticket/index";
    }

    @RequestMapping("ticket/detail")
    public String detail(Model model,Long tid, @ModelAttribute("qo")TouristQuery qo){
        //tid 景点id
        //通过景点id查询景点对象
        Tourist tourist = touristService.listByTouristId(tid);
        model.addAttribute("ticket",tourist);

        //通过景点id 查询对应的门票集合
        List<Ticket> ticket = ticketService.listByTouristId(tid);
        model.addAttribute("tickets",ticket);

        //查询必游的前5个景点
        List<Scenicspot> top5 = scenicspotService.getTop5();
        model.addAttribute("top5",top5);

        List<Tourist> ticketsTop5 = touristService.listForList(qo);
        if(ticketsTop5.size() > 5){
            ticketsTop5 = ticketsTop5.subList(0, 5);
        }
        model.addAttribute("ticketsTop5",ticketsTop5);

        return "ticket/detail";
    }

    @RequestMapping("ticket/addOrder")
    public String addOrder(Model model,Long tid){
        //tid 门票id
        //ticketDetail 通过门票id查询出对应的门票对象
        Ticket ticket = ticketService.listById(tid);
        model.addAttribute("ticket",ticket);

        //ticket    通过门票id查询出对应的景点对象
        Long tourist_id = ticket.getTourist_id();
        Tourist tourist = touristService.listByTouristId(tourist_id);
        model.addAttribute("ticket1",tourist);

        return "ticket/addOrder";
    }

    @RequestMapping("homePage/reloadSubAndProd")
    public String reloadSubAndProd(Model model,Long ajaxDestId, @ModelAttribute("qo")TouristQuery qo){
//        List<Tourist> list = touristService.listForList(qo);
        //通过目的地id查询该目的地下的标签
        List<Subject> subject = subjectService.selectByDestId(ajaxDestId);
        model.addAttribute("subjects",subject);

        //通过目的地id 查询景点
        Long sid = subject.get(0).getId();
        List<Tourist> list = touristService.listByDestIdAndSubjectId(ajaxDestId,sid);
        if(list.size() > 8){
            list = list.subList(0, 8);
        }
        model.addAttribute("tickets",list);


        return "ticket/subjectTpl";
    }

    @RequestMapping("homePage/reloadProd")
    public String reloadProd(Long ajaxDestId,Long ajaxSubjectId,Model model){
        //通过目的地id 和 标签id 查询景点
        List<Tourist> list = touristService.listByDestIdAndSubjectId(ajaxDestId,ajaxSubjectId);
        System.out.println(list);
        model.addAttribute("tickets",list);
        return "ticket/subjectTp2l";
    }
}
